package com.peration.cropdiagnosis;

import java.util.ArrayList;

import com.peration.connect.Sender;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

public class DiagTypeSelect extends Activity implements OnClickListener {
	Spinner spinner = null;
	public static String CurrentDiagType = "";
	ArrayList<String> typesList = new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.diag_type_select);
		spinner = (Spinner) findViewById(R.id.diag_types);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, getDiagTypeArray());
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		((Button) findViewById(R.id.button_next)).setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_next:
			 int selIndex = spinner.getSelectedItemPosition();
			 CurrentDiagType = typesList.get(selIndex);
			 startActivity(new Intent(v.getContext(), Diagnosis.class));
			 break;
		}
	}

	private String[] getDiagTypeArray() {
		String res = Sender.fetchUrl("diag_types");
		Log.i("Diag types from server", res);
		String types[] = res.split("::::");
		String[] typeLabels = new String[types.length];
		for (int k = 0; k < types.length; k++) {
			if (types[k].length() > 0) {
				Log.i("Diag type", types[k]);
				String[] typeDetails = types[k].split("----");
				typesList.add(typeDetails[0]);
				typeLabels[k]=typeDetails[1];
			}
		}

		return typeLabels;
	}
}